package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"zg3zk3/config"
	"zg3zk3/model"
)

var DB *gorm.DB

func MysqlInit() {
	// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
	var err error
	data := config.Conf.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", data.User, data.Password, data.Host, data.Port, data.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("mysql连接失败")
	}
	fmt.Println("mysql连接成功")
	DB.AutoMigrate(&model.User{}, &model.Article{}, &model.Follow{}, &model.QunUser{}, &model.Qun{}, &model.Message{})
}
